package com.um.jdy.platform.order.manager;

import cn.hutool.core.date.DateUtil;
import com.um.jdy.common.charge.service.*;
import com.um.jdy.platform.order.entity.vo.OrderDeviceTypeItemVO;
import com.um.jdy.platform.order.entity.vo.OrderOverviewVO;
import com.um.jdy.platform.order.entity.vo.OrderStationTypeItemVO;
import com.um.jdy.platform.order.entity.vo.OverviewOrderItemVO;
import com.um.jdy.platform.shop.entity.vo.ShopOverviewItemVO;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Component
@Slf4j
public class OrderOverviewManager extends BaseManager<OrderService> {

    /**
     * 订单概览图表
     * @return
     */
    public OrderOverviewVO getOverviewData(){

        Date date = new Date();
        String comId = getComId();
        String year = DateUtil.format(date,"yyyy");

        OrderOverviewVO orderOverviewVO = new OrderOverviewVO();

        //今天时间
        Date today = new Date();
        String todayBeginDate = DateUtil.format(today,"yyyy-MM-dd")+" 00:00:00";
        String todayEndDate = DateUtil.format(today,"yyyy-MM-dd")+" 23:59:59";

        OverviewOrderItemVO todayOrderGmv = service.getViewModel("get_order_overview_gmv",OverviewOrderItemVO.class,new Object[]{comId,todayBeginDate,todayEndDate});
        OverviewOrderItemVO todayOrder = service.getViewModel("get_order_overview_valid",OverviewOrderItemVO.class,new Object[]{comId,todayBeginDate,todayEndDate});

        //昨天时间
        Date yestoday = new Date();
        yestoday.setDate(today.getDate() - 1);
        String yestodayBeginDate = DateUtil.format(yestoday,"yyyy-MM-dd")+" 00:00:00";
        String yestodayEndDate = DateUtil.format(yestoday,"yyyy-MM-dd")+" 23:59:59";

        OverviewOrderItemVO yestodayOrderGmv = service.getViewModel("get_order_overview_gmv",OverviewOrderItemVO.class,new Object[]{comId,yestodayBeginDate,yestodayEndDate});
        OverviewOrderItemVO yestodayOrder = service.getViewModel("get_order_overview_valid",OverviewOrderItemVO.class,new Object[]{comId,yestodayBeginDate,yestodayEndDate});

        //本月
        Date month = new Date();
        String monthBeginDate = cn.hutool.core.date.DateUtil.beginOfMonth(month).toString();
        String monthEndDate = cn.hutool.core.date.DateUtil.endOfMonth(month).toString();

        OverviewOrderItemVO monthOrderGmv = service.getViewModel("get_order_overview_gmv",OverviewOrderItemVO.class,new Object[]{comId,monthBeginDate,monthEndDate});
        OverviewOrderItemVO monthOrder = service.getViewModel("get_order_overview_valid",OverviewOrderItemVO.class,new Object[]{comId,monthBeginDate,monthEndDate});

        //上月
        Date last = new Date();
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(last);
        calendar.add(Calendar.MONTH,-1);
        last = calendar.getTime();

        String lastBeginDate = DateUtil.beginOfMonth(last).toString();
        String lastEndDate = DateUtil.endOfMonth(last).toString();

        OverviewOrderItemVO lastOrderGmv = service.getViewModel("get_order_overview_gmv",OverviewOrderItemVO.class,new Object[]{comId,lastBeginDate,lastEndDate});
        OverviewOrderItemVO lastOrder = service.getViewModel("get_order_overview_valid",OverviewOrderItemVO.class,new Object[]{comId,lastBeginDate,lastEndDate});

        Integer exceptionCount = service.getCountBySqlName("get_order_overview_exception_count",new Object[]{comId}).intValue();
        Integer complaintNum = service.getCountBySqlName("get_order_overview_complaint_count",new Object[]{comId}).intValue();


        List<OrderDeviceTypeItemVO> deviceTypeList = service.getViewModelList("get_order_overview_device_type_list",OrderDeviceTypeItemVO.class,new Object[]{comId,year,year});
        Integer sum  = deviceTypeList.stream().mapToInt(OrderDeviceTypeItemVO::getNum).sum();
        for(OrderDeviceTypeItemVO item : deviceTypeList){
            item.setRadio(item.getNum() * 1.0 / sum * 100);
        }

        List<OrderStationTypeItemVO> stationTypeList = service.getViewModelList("get_order_overview_station_type_list",OrderStationTypeItemVO.class,new Object[]{comId,year,year});
        Integer sum1  = stationTypeList.stream().mapToInt(OrderStationTypeItemVO::getNum).sum();
        for(OrderStationTypeItemVO item : stationTypeList){
            item.setRadio(item.getNum() * 1.0 / sum1 * 100);
        }


        orderOverviewVO.setTodayGmv(todayOrderGmv.getAmount());
        orderOverviewVO.setTodayNum(todayOrder.getNum());
        orderOverviewVO.setTodayPrice(todayOrder.getAmount());
        orderOverviewVO.setTodayProfit(todayOrder.getProfit());

        orderOverviewVO.setYestodayGmv(yestodayOrderGmv.getAmount());
        orderOverviewVO.setYestodayNum(yestodayOrder.getNum());
        orderOverviewVO.setYestodayPrice(yestodayOrder.getAmount());
        orderOverviewVO.setYestodayProfit(yestodayOrder.getProfit());

        orderOverviewVO.setDayDifferGmv(todayOrderGmv.getAmount() - yestodayOrderGmv.getAmount());
        orderOverviewVO.setDayDifferNum(todayOrder.getNum() - yestodayOrder.getNum());
        orderOverviewVO.setDayDifferPrice(todayOrder.getAmount() - yestodayOrder.getAmount());
        orderOverviewVO.setDayDifferProfit(todayOrder.getProfit() - yestodayOrder.getProfit());

        orderOverviewVO.setMonthGmv(monthOrderGmv.getAmount());
        orderOverviewVO.setMonthNum(monthOrder.getNum());
        orderOverviewVO.setMonthPrice(monthOrder.getAmount());
        orderOverviewVO.setMonthProfit(monthOrder.getProfit());

        orderOverviewVO.setLastGmv(lastOrderGmv.getAmount());
        orderOverviewVO.setLastNum(lastOrder.getNum());
        orderOverviewVO.setLastPrice(lastOrder.getAmount());
        orderOverviewVO.setLastProfit(lastOrder.getProfit());

        orderOverviewVO.setMonthDifferGmv(monthOrderGmv.getAmount() - lastOrderGmv.getAmount());
        orderOverviewVO.setMonthDifferNum(monthOrder.getNum() - lastOrder.getNum());
        orderOverviewVO.setMonthDifferPrice(monthOrder.getAmount() - lastOrder.getAmount());
        orderOverviewVO.setMonthDifferProfit(monthOrder.getProfit() - lastOrder.getProfit());

        orderOverviewVO.setExceptionNum(exceptionCount);
        orderOverviewVO.setComplaintNum(complaintNum);

        orderOverviewVO.setDeviceTypeList(deviceTypeList);
        orderOverviewVO.setStationTypeList(stationTypeList);

        return orderOverviewVO;
    }



}
